import request from '@/utils/request'
import Vue from 'vue'
import axios from 'axios'
// 商品列表
const CancelToken = axios.CancelToken
Vue.prototype.$httpRequestList = [] // vue添加原型属性，记录请求信息, 也可以是数组[]用forEach批量撤销请求
export const getGoods = params => request({
  url: 'goods',
  method: 'GET',
  params,
  // 中断请求
  cancelToken: new CancelToken(function executor (c) {
    // console.dir(c) // 这个参数 c 就是CancelToken构造函数里面自带的取消请求的函数，这里把该函数当参数用
    Vue.prototype.$httpRequestList.push(c)
    // console.log(Vue.prototype.$httpRequestList)
  })
})

// 商品列表
export const deleteGoods = id => request({
  url: `goods/${id}`,
  method: 'DELETE'
})

// 商品数据
export const getGoodsData = id => request({
  url: `goods/${id}`,
  method: 'GET'
})

// 添加新商品
export const postGoods = data => request({
  url: 'goods',
  method: 'POST',
  data
})

// 修改商品数据
export const putGoodsData = (id, data) => request({
  url: `goods/${id}`,
  method: 'PUT',
  data
})

// 商品分类
export const getCategories = (params) => request({
  url: 'categories',
  method: 'GET',
  params,
  // 中断请求
  cancelToken: new CancelToken(function executor (c) {
    // console.dir(c) // 这个参数 c 就是CancelToken构造函数里面自带的取消请求的函数，这里把该函数当参数用
    Vue.prototype.$httpRequestList.push(c)
    // console.log(Vue.prototype.$httpRequestList)
  })
})

// 参数列表动态
export const getCategoriesMany = (id) => request({
  url: `categories/${id}/attributes`,
  method: 'GET',
  params: {
    sel: 'many'
  }
})

// 参数列表静态
export const getCategoriesOnly = (id) => request({
  url: `categories/${id}/attributes`,
  method: 'GET',
  params: {
    sel: 'only'
  }
})

// 上传图片
export const postPhoto = (data) => request({
  url: 'upload',
  method: 'POST',
  data
})

// 参数列表静态
export const poshCategoriesMany = (attrId, name, vals, id) => request({
  url: `categories/${id}/attributes/${attrId}`,
  method: 'PUT',
  data: {
    attr_sel: 'many',
    attr_name: name,
    attr_vals: vals
  }
})

// 参数列表静态
export const poshCategoriesOnly = (attrId, name, vals, id) => request({
  url: `categories/${id}/attributes/${attrId}`,
  method: 'PUT',
  data: {
    attr_sel: 'only',
    attr_name: name,
    attr_vals: vals
  }
})

// 参数列表静态
export const deleteParams = (attrId, id) => request({
  url: `categories/${id}/attributes/${attrId}`,
  method: 'DELETE'
})

// 添加静态或动态参数
export const postParamsMany = (id, name) => request({
  url: `categories/${id}/attributes`,
  method: 'POST',
  data: {
    attr_name: name,
    attr_sel: 'many'
  }
})

// 添加静态或动态参数
export const postParamsOnly = (id, name) => request({
  url: `categories/${id}/attributes`,
  method: 'POST',
  data: {
    attr_name: name,
    attr_sel: 'only'
  }
})

// 添加新分类
export const postCategories = (data) => request({
  url: 'categories',
  method: 'POST',
  data
})

// 删除分类
export const deleteCategories = (id) => request({
  url: `categories/${id}`,
  method: 'DELETE'
})

// 修改分类
export const putCategories = (id, name) => request({
  url: `categories/${id}`,
  method: 'PUT',
  data: {
    cat_name: name
  }
})
